let customeraddModule = (() => {
  let
    customerId,
    username = $('.username'),
    man = $('#man'),
    woman = $('#woman'),
    useremail = $('.useremail'),
    userphone = $('.userphone'),
    userqq = $('.userqq'),
    userweixin = $('.userweixin'),
    inpBox_type = $('.inpBox').find('select'),
    inpBox_address = $('.inpBox').find('textarea'),
    submit = $('.submit')


  // 编辑进入
  let judge = async function () {
    if (customerId) {
      let
        res = await axios.get('/customer/info', { params: { customerId } }),
        {
          id,
          name,
          sex,
          email,
          phone,
          QQ,
          weixin,
          type,
          address,
          userId,
          userName
        } = res.data;
      username.val(name);
      sex == 0 ? man.prop('checked', true) : woman.prop('checked', true);
      useremail.val(email);
      userphone.val(phone);
      userqq.val(QQ);
      userweixin.val(weixin);
      inpBox_type.val(type);
      inpBox_address.val(address);
      return
    }
  }
  // 绑定提交事件
  let handleSubmit = function () {
    let deliver = async function () {
      // 收集表单数据
      let params = {
        name: username.val().trim(),
        sex: man.prop('checked') ? 0 : 1,
        email: useremail.val().trim(),
        phone: userphone.val().trim(),
        QQ: userqq.val().trim(),
        weixin: userweixin.val().trim(),
        type: inpBox_type.val().trim(),
        address: inpBox_address.val().trim()
      }
      if (customerId) {
        // 编辑
        params.customerId = customerId
        let res = await axios.post('/customer/update', params)
        if (res.code) {
          alert('修改客户失败')
          return;
        }
        alert('修改客户成功')
        window.location.href = 'customerlist.html'
      } else {
        // 添加
        let res = await axios.post('/customer/add', params)
        if (res.code) {
          alert('添加客户失败')
          return;
        }
        alert('添加客户成功')
        window.location.href = 'customerlist.html'
      }
    };
    submit.click(deliver)
  }
  return {
    init() {
      customerId = window.location.href.getUrlEncodedParams().customerId
      judge()
      handleSubmit()
    }
  }
})()

customeraddModule.init()